package com.neborosoft.lib.collections.queue;

import org.apache.commons.collections15.buffer.CircularFifoBuffer;

import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 07.01.13
 * Time: 13:01
 * To change this template use File | Settings | File Templates.
 */
public class FixedSizeQueue<T> extends CircularFifoBuffer<T>{
    public static interface OnElementRemovedListener<T>{
        void onRemove(T element);
    }

    private OnElementRemovedListener<T> onElementRemovedListener;

    public OnElementRemovedListener<T> getOnElementRemovedListener() {
        return onElementRemovedListener;
    }

    public void setOnElementRemovedListener(OnElementRemovedListener<T> onElementRemovedListener) {
        this.onElementRemovedListener = onElementRemovedListener;
    }

    public FixedSizeQueue() {

    }

    public FixedSizeQueue(int size) {
        super(size);
    }

    public FixedSizeQueue(Collection<T> coll) {
        super(coll);
    }

    @Override
    public T remove() {
        T objectToRemove = super.remove();
        if(objectToRemove != null){
            if(onElementRemovedListener != null){
                onElementRemovedListener.onRemove(objectToRemove);
            }
        }

        return objectToRemove;
    }
}
